<?php

/*
 * This class is used for as Model for Product table 
 */

class Model_Enoria extends Model_Base {

    protected $_name = 'enoria';
    protected $_primaryKey = 'en_id';

    public function getStatusArray() {
        return array(
            "" => "-- Select Status --",
            "1" => "Active",
            "0" => "Inactive"
        );
    }
    
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array("en" => "enoria"))
                ->where("en_id = ?", $id)
                ->where("en_status!= 2");
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
    public function fetchEntryByREFId($id) {
        $select = $this->select()
                ->from(array("en" => "enoria"))
                ->where("en_ref_id = ?", $id)
                ->where("en_status!= 2");
        $data = $this->fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
    /**
     *  This function is used to get Questions
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEnoria($where = "", $limit = "", $offset = "0", $order = 'en_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('enoria' => 'enoria'))
                ->where("en_status!= 2");
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);

        $data = parent::fetchAll($select);
        if (!empty($data)) {
            $finalArray = array('' => '-- Select Enoria --');
            foreach ($data->toArray() as $_area_type) {
                $finalArray[$_area_type['en_id']] = $_area_type['en_title'];
            }
            return $finalArray;
        } else {
            return array('' => '-- Select Enoria --');
        }
    }
    
    public function fetchAllEnoria($where = "", $limit = "", $offset = "0", $order = 'en_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('enoria' => 'enoria'))
                ->where("en_status!= 2");
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);

        $data = parent::fetchAll($select);
        $finalArray = array();
        if (!empty($data)) {
            foreach ($data->toArray() as $_area_type) {
                $finalArray[$_area_type['en_id']] = $_area_type['en_title'];
            }
            return $finalArray;
        } else {
            return array();
        }
    }
    
    /**
     *  This function is used to get enoria
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchLISTEnoria($where = "", $limit = "", $offset = "0", $order = "en_id", $sort = "ASC" ,$group = 'en_ref_id') {
        $select = $this->select()
                ->from(array("en" => "enoria"),array('*','lang_names' => new Zend_Db_Expr('GROUP_CONCAT(ln.lang_name)')))
                ->setIntegrityCheck(false)
                ->join(array('ln' => 'languages'), "en.en_lang_id = ln.lang_id",array('lang_name'))
                ->join(array('are' => 'area'), "are.area_id = en.area_id",array('area_name'))
                ->where("en_status != 2")
                ->where("en_name != ''");
        if ($where != "") {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        if ($group != "") {
            $select->group($group);
        }
        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getEnoriaCount($where) {
        $select = $this->select()
                ->from(array("en" => "enoria"), Array("cnt" => "count(*)"))
                ->where("en_status != 2")
                ->where("en_name != ''");
                #->group("en_ref_id");
        if ($where != "") {
            $select->where($where);
        }
        #echo $select;exit;
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }
    /**
     *  This function is used to delete record
     * 
     */
    public function deleteEnoria($enArray) {
        if (is_array($enArray)) {
            foreach ($enArray as $_enoria) {
                $enoriaData = Array();
                $enoriaData = $this->fetchEntryByREFId($_enoria);
                if (count($enoriaData) > 0) {
                    $data = array("en_status" => "2");
                    $this->update($data, "en_ref_id =" . (int) $_enoria);
                }
            }
        } else {
            $en_id = $enArray;
            $enoriaData = $this->fetchEntryByREFId($en_id);
            if (count($enoriaData) > 0) {
                $data = array("en_status" => "2");
                $this->update($data, "en_ref_id =" . $en_id);
            }
        }
    }

}
